package com.bpc.cs.dao;

import java.util.List;

import org.hibernate.criterion.Order;

import com.bpc.cs.dto.FactorDTO;

/**
 * @author   phan_tp
 * @datetime Dec 7, 2012 : 5:34:10 PM
 */
public class FactorDAO extends BaseDAO implements IFactorDAO {

	/**
	 * Get list factor
	 * @return List<FactorDTO>
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FactorDTO> selectAllFactor() {
		List<FactorDTO> listFactorDTO = getSession().createCriteria(FactorDTO.class)
													.addOrder(Order.asc("factorName"))
													.list();
		return listFactorDTO;
	}

	/**
	 * Select factor by factorID
	 * @param int factorID
	 * @return FactorDTO
	 */
	public FactorDTO selectFactorByID(int factorID) {
		return (FactorDTO)getSession().createQuery("FROM FactorDTO WHERE factorID = ?")
											 .setParameter(0, factorID)
											 .list()
											 .get(0);
	}

	/**
	 * Insert new factor to table
	 * @param FactorDTO factorDTO
	 * @return Boolean TRUE : Insert successful
	 * 				   FALSE: Insert failed
	 */
	@Override
	public boolean insertFactor(FactorDTO factorDTO) {
		getSession().save(factorDTO);
		return true;
	}

	/**
	 * Check factor name is existed or not
	 * @param String factorName
	 * @return boolean TRUE : factor name is duplicated
	 * 				   FALSE: factor name isn't duplicated
	 */
	public boolean isDuplicateFactorName(String factorName) {
		return 0 < ((Number)getSession().createQuery("SELECT COUNT(*) FROM FactorDTO WHERE factorName = ?")
										.setParameter(0, factorName)
										.uniqueResult())
										.intValue();
	}
}
